#include "RootScene.h"
#include "Tower.h"
#include "WalkPoint.h"
#include "Enemy.h"

using namespace cocos2d;

CRootScene::CRootScene()
{
	arrTowers = CCArray::create();
	arrTowers->retain();

	arrEnemys = CCArray::create();
	arrEnemys->retain();
}

CRootScene::~CRootScene()
{
	arrTowers->release();
	arrEnemys->release();
}

// on "init" you need to initialize your instance
bool CRootScene::init()
{
    bool bRet = false;
    do 
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////

        CC_BREAK_IF(! CCLayer::init());

		this->setTouchEnabled(true);
		CCSize wins = CCDirector::sharedDirector()->getWinSize();
		CCSprite *background = CCSprite::create("Bg.png");
		this->addChild(background);
		background->setPosition(ccp(wins.width / 2, wins.height / 2));

		loadTowerPositions();
		loadWalkPoints();

		ui_wave_lbl = CCLabelBMFont::create( CCString::createWithFormat("WAVE: %d", 0)->getCString(), "font_red_14.fnt");
		addChild(ui_wave_lbl, 10);
		ui_wave_lbl->setPosition(ccp(400, wins.height - 12));
		ui_wave_lbl->setAnchorPoint(ccp(0, 0.5));
		mWave = loadWave( 0 );
        bRet = true;
    } while (0);

    return bRet;
}

void CRootScene::loadTowerPositions()
{
	CCArray* towerPositions = CCArray::createWithContentsOfFile("TowersPosition.plist");
	arrTowerBases = CCArray::createWithCapacity(10);
	arrTowerBases->retain();

	CCObject *pObject = NULL;
	CCARRAY_FOREACH(towerPositions, pObject)
	{
		CCDictionary* towerPos = (CCDictionary*)pObject;
		CCSprite* towerBase = CCSprite::create("open_spot.png");
		this->addChild(towerBase);
		towerBase->setPosition(ccp(((CCString*)towerPos->objectForKey("x"))->intValue(),
			((CCString*)towerPos->objectForKey("y"))->intValue()));
		arrTowerBases->addObject(towerBase);
	}
}

int CRootScene::loadWave( int nWave )
{
	CCArray *waveData = CCArray::createWithContentsOfFile("Waves.plist");
	if ( nWave >= waveData->count() )
	{
		return -1;
	}

	CCArray *currentWaveData = (CCArray*)waveData->objectAtIndex( nWave );
	CCObject *pObject = NULL;
	CCARRAY_FOREACH(currentWaveData, pObject)
	{
		CCDictionary* enemyData = (CCDictionary*)pObject;
		CWalkPoint* pPoint = ((CWalkPoint*)arrWalkPoints->objectAtIndex(0));
		CEnemy *pEnemy = CreateEnemy( this, pPoint->getPosition(), ((CCString*)enemyData->objectForKey("spawnTime"))->floatValue() );
		arrEnemys->addObject( pEnemy );
	}

	ui_wave_lbl->setString(CCString::createWithFormat("WAVE: %d", nWave)->getCString());
	return ++nWave;
}

void CRootScene::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
	CCSetIterator iter = pTouches->begin();
	for (; iter != pTouches->end(); iter++)
	{
		CCTouch* pTouch = (CCTouch*)(*iter);
		CCPoint location = pTouch->getLocation();

		CCObject *pObject = NULL;
		CCARRAY_FOREACH(arrTowerBases, pObject)
		{
			CCSprite *tb = (CCSprite*)pObject;
			if (this->canBuyTower() && tb->boundingBox().containsPoint(location) && !tb->getUserData())
			{
				//We will spend our gold later.

				CTower* tower = CreateTower( this, tb->getPosition());
				arrTowers->addObject(tower);
				tb->setUserData(tower);
			}
		}
	}
}

bool CRootScene::canBuyTower() const
{
	return true;
}

bool judgeCollision(CCPoint circlePoint, float radius, CCPoint circlePointTwo, float radiusTwo)
{
	float xdif = circlePoint.x - circlePointTwo.x;
	float ydif = circlePoint.y - circlePointTwo.y;

	float distance = sqrt(xdif * xdif + ydif * ydif);

	return (distance <= radius + radiusTwo);
}

void CRootScene::loadWalkPoints()
{
	arrWalkPoints = CCArray::create();
	arrWalkPoints->retain();

	CWalkPoint *waypoint6 = CreateWalkPoint(this, ccp(-40, 220));
	arrWalkPoints->addObject(waypoint6);

	CWalkPoint *waypoint5 = CreateWalkPoint(this, ccp(445, 220));
	arrWalkPoints->addObject(waypoint5);

	CWalkPoint *waypoint4 = CreateWalkPoint(this, ccp(445, 130));
	arrWalkPoints->addObject(waypoint4);

	CWalkPoint *waypoint3 = CreateWalkPoint(this, ccp(35, 130));
	arrWalkPoints->addObject(waypoint3);

	CWalkPoint *waypoint2 = CreateWalkPoint(this, ccp(35, 35));
	arrWalkPoints->addObject(waypoint2);

	CWalkPoint *waypoint1 = CreateWalkPoint(this, ccp(420, 35));
	arrWalkPoints->addObject(waypoint1);

}

bool CRootScene::getNextWalkTarget( CCPoint& current )
{
	bool bFound = 0;
	CCObject* pObject = NULL;
	CCARRAY_FOREACH( arrWalkPoints, pObject )
	{
		CWalkPoint* pPoint = (CWalkPoint*)pObject;

		if( bFound )
		{
			current = pPoint->getPosition();
			return true;
		}

		if( pPoint->getPosition().equals( current ) )
		{
			bFound = true;
			continue;
		}
	}

	return false;
}
